/**
** usage：转换设置运营参数后的模型为最终的页面数据定义模型（包含运营参数、约束条件等），便于进行内容编辑、录入；

//init stage:
// load main type def
**/
var pageMetaParam = null;

function loadPageParams(){

	//load page mode  
	//var pageMeta = null;
	//pageMeta = fetchUrlContent("/4gpageMeta.json");
	pageMetaParam = JSON.parse(fetchPageMetaParam());
	return transToPageModeMeta2(pageMetaParam,pageMeta);
}
 
function fetchPageMetaParam(){
	return null;
}

//transform page mode to actual page schema obj:
function transToPageModeMeta2(pageParam,rawMetaObj){ 
	var pageMeta2={};
	pageMeta2["type"] ="object"; 
	for(var k in rawMetaObj){
		if(k === "pageBlocks"){ 
			if(!pageMeta2["properties"]) pageMeta2["properties"] = {}; 
			pageMeta2["properties"]['pageBlocks'] = {};

			pageMeta2["properties"]['pageBlocks']['title'] = "页面区域定义";
			pageMeta2["properties"]['pageBlocks']['type'] = "object";

			pageMeta2["properties"]['pageBlocks']["properties"] =  buildPageBlocks2(pageParam[k],rawMetaObj[k]);

		}
		else{
			if(k === "pageVars"){
				pageMeta2["properties"]["pageVars"] = buildArray2(pageParam[k],rawMetaObj[k]);
			}
			else{
				pageMeta2[k] = rawMetaObj[k];
			}
		}
	}
	
	console.log("page meta info:\n" + JSON.stringify(pageMeta2));
	return JSON.stringify(pageMeta2);
	//return false;
}


function buildPageBlocks2(blocksParam,rawPageBlocks){
	var ret = {};
	for(var b in rawPageBlocks){
		ret[b] = buildObject2(blocksParam[b],rawPageBlocks[b]);
	}
	return ret;
}

function buildObject2(paramDef,blockDef){ 
	var ret = {}; 
	for(var k in blockDef){
		var to = blockDef[k];
		if(k === "properties" ){
			if(to){
				ret[k] = {};
				for(var pk in to){	 //for each prop
					var prop = to[pk];
					if(prop["type"]){
						var t = prop["type"];
						if(t === "object"){
							ret[k][pk] = buildObject2(paramDef[pk],prop);
						}
						else
							if(t === "array"){
								ret[k][pk] = buildArray2(paramDef[pk],prop);
							}
							else if(t === "TArrayDef"){								
								var tobj = buildArray2(paramDef[pk],prop);
								tobj["type"] = "array";
								ret[k][pk] = tobj;
								//ret[k][pk] = buildBuildInArrayType(prop);	
							}
							else{
								//prop is buildin type:to transform to standart json schema type
								var bp = transStandartType(paramDef[pk],prop);
								ret[k][pk] = bp;
							}
					}
				}
			}
			else
				ret[k] = b[k];
		}
		else{
			ret[k] = to;
		}
	}
	if(blockDef["title"]) ret["title"] = blockDef["title"];
	ret["type"] = "object";

	return ret;
}

function buildArray2(paramDef,prop){
	var ret = {};
	for(var i in prop){
		if(i === "items"){
			ret[i] = buildObject2(paramDef,prop[i]);
		}
		else if(i === 'properties'){	//treate TArrayDef ...		
			ret['items'] = buildObject2(paramDef,prop[i]['items']);
			buildArrayObj(ret,paramDef,prop[i]);
		}
		else{
			ret[i] = prop[i];
		} 
	} 
	ret['type'] = "array";
	return ret;
}

function buildArray3(paramDef,prop){
	var ret = {};
	for(var i in prop){
		if(i === "items"){
			ret[i] = buildObject2(paramDef,prop[i]);
		}
		else{
			ret[i] = prop[i];
		}
	} 
	ret['type'] = "array";
	return ret;
}
function transStandartType(paramDef,prop){ 
	var bp = {};
	var type = prop['type'];
	for(var tpk in prop){
		bp[tpk] = prop[tpk];
	}
	if(type === 'TTextDef'){
		buildTextObj(bp,paramDef,prop);
	}
	else if(type === 'TPictureDef'){		
		bp['format'] = 'url';
		bp['type'] = 'string';
	}
	else if(type === 'TLinkDef'){
		bp['format'] = 'url';
		bp['type'] = 'string';
	}
	else if(type === "TTextlinkDef"){
    	bp["type"]="object";
    	bp["properties"] = {};
    	var txtObj = {};
    	buildTextObj(txtObj,paramDef['linkTitle'],prop);
    	bp['properties']['linkTitle'] = txtObj;  
    	bp['properties']['url'] = {    		
            "title":"链接地址",
            'type':"string"
    	};
    	if(paramDef['url'] && paramDef['url']['url'] )
    		bp['properties']['url']['default'] = paramDef['url']['url'];
    }  
	else if(type === 'TPicLinkDef'){
    	bp["type"]="object";
    	bp["properties"] = {    		
            "picture":{
                "title":"链接图片"
            },
            "url":{
                "title":"链接地址"
            }
    	};

	}

	return bp;
}

function buildArrayObj(bp,paramDef,prop){
	for(var k in paramDef){
		if(k === 'maxItems' || k === 'minItems'){
			bp[k] = paramDef[k];
		}
	}
}

function buildTextObj(bp,paramDef,prop){

		bp['type'] = 'string';
		for(var k in paramDef){
			if(k === 'texttype'){
				//TODO:set format ....
			}
			if(k === 'default') bp[k] = paramDef[k];
			if(k === 'maxLength') {
				if(paramDef[k][k]) bp[k] = paramDef[k][k];
				if(paramDef[k]['includeEdge']) {
					//TODO: set upper edge
				}
			}
			if(k == 'minLength'){			
				if(paramDef[k][k]) bp[k] = paramDef[k][k];
				if(paramDef[k]['includeEdge']) {
					//TODO: set down edge
				}	
			}
		}
}